<template>
  <div class="component-sign-management">
    <div class="search-bar">
      <el-form
        ref="searchFormRef"
        :inline="true"
        :model="searchForm"
        @submit.prevent
      >
        <div class="input-row">
          <el-form-item
            prop="shopName"
            :label="$t('prodList.shopName')+':'"
          >
            <el-input
              v-model="searchForm.shopName"
              clearable
              :placeholder="$t('prodList.shopName')"
            />
          </el-form-item>
          <el-form-item
            :label="$t('order.applicationTime')+':'"
          >
            <el-date-picker
              v-model="dateRange"
              type="datetimerange"
              clearable
              :range-separator="$t('date.tip')"
              value-format="YYYY-MM-DD HH:mm:ss"
              :start-placeholder="$t('date.start')"
              :end-placeholder="$t('date.end')"
            />
          </el-form-item>
          <el-form-item>
            <div
              class="default-btn primary-btn"
              @click="searchChange(true)"
            >
              {{ $t('product.search') }}
            </div>
            <div
              class="default-btn"
              @click="resetSearchForm"
            >
              {{ $t('product.reset') }}
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <div class="main-container">
      <div class="table-con audit-shop-table">
        <el-table
          ref="auditShopSign"
          :data="dataList"
          header-cell-class-name="table-header"
          style="width: 100%"
        >
          <el-table-column
            prop="shopName"
            :label="$t('prodList.shopName')"
          >
            <template #default="scope">
              <span class="table-cell-text">{{ scope.row.shopName }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="shopLogo"
            :label="$t('shop.shopLogo')"
          >
            <template #default="scope">
              <div
                v-if="scope.row.shopLogo && scope.row.shopLogo !== null"
                class="table-cell-image"
              >
                <ImgShow :src="scope.row.shopLogo" />
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="updateTime"
            :label="$t('order.applicationTime')"
          />
          <el-table-column
            :label="$t('publics.operating')"
            width="150"
          >
            <template #default="scope">
              <div
                v-if="isAuth('shop:sign:audit')"
                class="default-btn text-btn"
                @click="auditHandle(scope.row.shopId, scope.row.type)"
              >
                {{ $t('productComm.audit') }}
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        v-if="dataList.length"
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdateRef"
      @refresh-data-list="refreshChange"
    />
  </div>
</template>

<script setup>
import { onMounted } from 'vue'
import { isAuth } from '@/utils'
import AddOrUpdate from './add-or-update.vue'

let tempSearchForm = null // 保存上次点击查询的请求条件
const dataList = ref([])
const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})

const searchForm = ref({
  shopName: undefined
}) // 搜索
const dateRange = ref([])

onMounted(() => {
  getDataList()
})

// 分页获取待审核的签约信息
const getDataList = (paramPage, newData = false) => {
  const tempPage = paramPage || page
  if (newData || !tempSearchForm) {
    tempSearchForm = {
      current: tempPage.currentPage,
      size: tempPage.pageSize,
      status: 0,
      beginTime: dateRange.value ? dateRange.value[0] : null, // 开始时间
      endTime: dateRange.value ? dateRange.value[1] : null // 结束时间
    }
  } else {
    tempSearchForm.current = tempPage.currentPage
    tempSearchForm.size = tempPage.pageSize
  }
  // status 0 未审核 1已通过 -1未通过 不传返回全部
  http({
    url: http.adornUrl('/platform/signingAuditing/page'),
    method: 'get',
    params: http.adornParams(Object.assign(tempSearchForm, searchForm.value
    ))
  }).then(({ data }) => {
    dataList.value = data.records
    page.total = data.total
    page.currentPage = data.current
  })
}
// 查看 / 审核
const addOrUpdateVisible = ref(false)
const addOrUpdateRef = ref(null)
const auditHandle = (id, type) => {
  addOrUpdateVisible.value = true
  nextTick(() => {
    addOrUpdateRef.value.init(id, type)
  })
}

const refreshChange = () => {
  page.currentPage = 1
  getDataList(page)
}

const searchChange = (newData = false) => {
  page.currentPage = 1
  getDataList(page, newData)
}

const searchFormRef = ref(null)
const resetSearchForm = () => {
  searchFormRef.value.resetFields()
  searchForm.value = {}
  dateRange.value = []
}

const handleSizeChange = (val) => {
  page.pageSize = val
  getDataList()
}
const handleCurrentChange = (val) => {
  page.currentPage = val
  getDataList()
}
</script>
